package com.haifeng.asms.api.config;

import com.haifeng.asms.api.handler.RedisCacheErrorHandler;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * <p>
 *  缓存Key生成类
 * </p>
 *
 * @author: Haifeng
 * @date: 2020-06-21
 */
@Configuration
public class CacheConfig extends CachingConfigurerSupport {

    @Bean("cacheKeyGenerator")
    public KeyGenerator cacheKeyGenerator(){
        return (target, method, params) ->
                target.getClass().getName()+"_"
                        +method.getName()+"_"
                        + Arrays.asList(params);
    }

    @Override
    public CacheErrorHandler errorHandler() {
        return new RedisCacheErrorHandler();
    }
}
